
package net.atremmedia.gwt.actionjava.examples.basics.samples;

import net.atremmedia.gwt.actionjava.examples.AJExample;
import net.atremmedia.gwt.actionjava.examples.frame.AJExampleOverview;
import actionjava.display.Graphics;
import actionjava.display.Sprite;
import actionjava.geom.Color;
import actionjava.geom.Point2D;

public class MultiCurves3 extends AJExample
{
	public static final String NAME = "Multi curves 3";
	public static final String DESCRIPTION = "Description";
	public static final String EXAMPLE_SRC = "https://code.google.com/p/gwt-actionjava/source/browse/trunk/aj-examples/aj-examples-basics/src/main/java/net/atremmedia/gwt/actionjava/examples/basics/samples/MultiCurves3.java";

	private int numPoints = 9;
	private Sprite curveSprite;
	private Graphics curveGraphs;

	public MultiCurves3(AJExampleOverview base)
	{
		super(NAME, DESCRIPTION, EXAMPLE_SRC, base);
	}

	@Override
	public void run()
	{
		curveSprite = new Sprite();
		curveGraphs = curveSprite.getGraphics();

		Point2D[] points = new Point2D[numPoints];
		for(int i = 0;i < numPoints;i++) {
			points[i] = new Point2D((Math.random() * getWidth()), (Math.random() * getHeight()));
		}

		double xc1 = (points[0].x + points[numPoints - 1].x) / 2;
		double yc1 = (points[0].y + points[numPoints - 1].y) / 2;
		curveGraphs.beginStroke(Color.BLACK.getHexValue());
		curveGraphs.setStrokeStyle(1);
		curveGraphs.moveTo(xc1, yc1);

		double xc;
		double yc;
		for(int i = 0;i < numPoints - 1;i++) {
			xc = (points[i].x + points[i + 1].x) / 2;
			yc = (points[i].y + points[i + 1].y) / 2;
			curveGraphs.curveTo(points[i].x, points[i].y, xc, yc);
		}
		curveGraphs.curveTo(points[points.length - 2].x, points[points.length - 2].y, xc1, yc1);

		addChild(curveSprite);
	}
}
